package p2pcdnsim.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import p2pcdnsim.cdn.LFUCaching;
import p2pcdnsim.cdn.ObjectContent;



public class TestLFUCaching {
	
	LFUCaching<ObjectContent> lfu = new LFUCaching<ObjectContent>();
	
	@Test
	public void testInsertObject() {
		this.lfu.setCacheSize(10);
		//Teste
		ObjectContent content = new ObjectContent(1,10);
		ObjectContent content2 = new ObjectContent(2,5);
		
		this.lfu.insertObject(content2, null);
		this.lfu.retrieveObject(2, null);
		this.lfu.retrieveObject(2, null);
		this.lfu.retrieveObject(2, null);
		
		ObjectContent content3 = new ObjectContent(3,3);
		
		this.lfu.insertObject(content3, null);
		this.lfu.retrieveObject(3, null);
		
		
		lfu.insertObject(content, null);
		
		assertNotNull(this.lfu.retrieveObject(2, null));
		assertNotNull(this.lfu.retrieveObject(3, null));
		assertNull(this.lfu.retrieveObject(1, null));
		
		//Teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		
		ObjectContent content4 = new ObjectContent(4,10);
		this.lfu.insertObject(content4, null);
		
		assertNotNull(this.lfu.retrieveObject(4, null));
		
		//Teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		
		ObjectContent content5 = new ObjectContent(5,11);
		assertFalse(this.lfu.insertObject(content5, null));
		
		//Teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		ObjectContent content6 = new ObjectContent(6,4);
		
		this.lfu.insertObject(content3, null);
		this.lfu.retrieveObject(3, null);
		
		this.lfu.insertObject(content6, null);
		
		assertNotNull(this.lfu.retrieveObject(3, null));
		assertNotNull(this.lfu.retrieveObject(6, null));
		
	}
	
	@Test
	public void testRetrieveObject() {
		
		//Novo teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		
		ObjectContent content = new ObjectContent(1,3);
		this.lfu.insertObject(content, null);
		this.lfu.retrieveObject(1, null);
		this.lfu.retrieveObject(1, null);
		this.lfu.retrieveObject(1, null);
		
		ObjectContent content2 = new ObjectContent(2, 5);
		this.lfu.insertObject(content2, null);
		this.lfu.retrieveObject(2, null);
		this.lfu.retrieveObject(2, null);
		this.lfu.retrieveObject(2, null);
		
		ObjectContent content3 = new ObjectContent(3,8);
		this.lfu.insertObject(content3, null);
		this.lfu.retrieveObject(3, null);
		this.lfu.retrieveObject(3, null);
		this.lfu.retrieveObject(3, null);
		
		assertNotNull(this.lfu.retrieveObject(1, null));
		assertNotNull(this.lfu.retrieveObject(2, null));
		assertNull(this.lfu.retrieveObject(3, null));
		
		this.lfu.retrieveObject(3, null);
		assertNull(this.lfu.retrieveObject(1, null));
		assertNull(this.lfu.retrieveObject(2, null));
		assertNotNull(this.lfu.retrieveObject(3, null));
		
		
		//Novo teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		
		ObjectContent content4 = new ObjectContent(4,6);
		this.lfu.insertObject(content4, null);
		this.lfu.retrieveObject(4, null);
		
		ObjectContent content5 = new ObjectContent(5,6);
		this.lfu.insertObject(content5, null);
		this.lfu.retrieveObject(5, null);
		this.lfu.retrieveObject(5, null);
		
		assertNull(this.lfu.retrieveObject(4, null));
		assertNotNull(this.lfu.retrieveObject(5, null));
				
		this.lfu.retrieveObject(4, null);
		this.lfu.retrieveObject(4, null);
		
		assertNull(this.lfu.retrieveObject(5, null));
		assertNotNull(this.lfu.retrieveObject(4, null));
		
		//Novo teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		
		this.lfu.insertObject(content, null);
		this.lfu.retrieveObject(1, null);
		this.lfu.retrieveObject(1, null);
		this.lfu.retrieveObject(1, null);
		this.lfu.retrieveObject(1, null);
		
		this.lfu.insertObject(content2, null);
		this.lfu.retrieveObject(2, null);
		this.lfu.retrieveObject(2, null);
		this.lfu.retrieveObject(2, null);
		
		this.lfu.insertObject(content3, null);
		this.lfu.retrieveObject(3, null);
		this.lfu.retrieveObject(3, null);
		
		assertNull(this.lfu.retrieveObject(3, null));
		assertNull(this.lfu.retrieveObject(3, null));
		assertNull(this.lfu.retrieveObject(3, null));
		assertNotNull(this.lfu.retrieveObject(3, null));
		
		//Novo teste
		this.lfu = new LFUCaching<ObjectContent>();
		this.lfu.setCacheSize(10);
		
		ObjectContent content6 = new ObjectContent(6,3);
		assertNotNull(this.lfu.insertObject(content6, null));
		assertNotNull(this.lfu.retrieveObject(6, null));
		
	}

}
